//应付管理
/**
 * fixed by zhaojian
 */
/**
 * 应付管理列表视图
 */
interface IFeeManagentViewModel extends IBaseViewModel<IFeeManagentQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<FinanceFeeListItemResponse>;
    /**
     * 查询
     */
    query: () => void;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     *费用短号
     */
    feeCode: string;
    /**
     *结算承运商
     */
    carrierName: string;
    /**
     *付款方式
     */
    paymentMethod: string;
    /**
     *合同号
     */
    contractNumber: string;
    /**
     *车牌号
     */
    carCode: string;
    /**
     *费用类型
     */
    feeType: string;
    /**
     *结算承运商电话
     */
    carrierPhone: string;
    /**
    *费用状态
    */
    feeStatus: string;
    /**
    *申领人员
    */
    applicant: string;
    /**
    *应付总编号
    */
    feeSettleBillCode: string;
    /**
     *客户单位名称
     */
    clientName: string;
    /**
     * 付款方式下拉
     */
    paymentMethodList: ValueListItemResponse[];
    /**
     * 费用类型下拉
     */
    feeTypeList: ValueListItemResponse[];
    /**
     * 费用状态下拉
     */
    feeStatusEnumList: ValueListItemResponse[];
    /**
     * 费用合计
     */
    CostTotal: string;
    /**
     * 导出报表显示按钮
     */
    canExcel: boolean;
    /**
     *跳转费用对账页面事件
     */
    checkDetail: () => void;
    /**
     * 导出报表事件
     */
    excel: () => void;
    /**
     * 列表高度
     */
    heightA:number;
    searchClient:(value:string) => any;
    /**
     * 选择客户单位事件
     */
    clientSelect: (selected:angucomplete<ClientListItemResponse>) => void;

    inputChanged:(str:string)=>void;

}


/**
 * 查询条件
 */
interface IFeeManagentQueryTerms {
    /**
     *费用短号
     */
    feeCode: string;
      /**
     * 客户单位名称
     */
    client:string
    /**
     *结算承运商
     */
    carrierName: string;
    /**
     *付款方式
     */
    paymentMethod: string;
    /**
     *合同号
     */
    contractNumber: string;
    /**
     * 申领起始时间
     */
    startTime:string;
    /**
     * 申领结束时间
     */
    endTime:string;
    /**
     *车牌号
     */
    carCode: string;
    /**
     *费用类型
     */
    feeType: string;
    /**
     *结算承运商电话
     */
    carrierPhone: string;
    /**
     * 费用状态
     */
    feeStatus:string;
    /**
    *申领人员
    */
    applicant: string;
    /**
    *应付总编号
    */
    feeSettleBillCode: string;
    /**
     * 结算起始时间
     */
    settleStartTime:string;
    /**
     * 结算结束时间
     */
    settleEndTime:string;
    /**
     * 付款起始时间
     */
    payStartTime:string;
    /**
     * 付款结束时间
     */
    payEndTime:string;
    /**
     *付款人员
     */
    payUser: string;

}


class FeeManagentController extends ListController<IFeeManagentViewModel, IFeeManagentQueryTerms>{
    gridApi: uiGrid.IGridApiOf<FinanceFeeListItemResponse>;
    /**
     * 排序字段枚举
     */
    orderBy: number = 0;
    /**
     * 是否升序
     */
    asc: boolean = true;
    /**
     * 费用编号id(标识符)
     */
    id: string;
    /**
     * 费用状态s
     */
    feeStatusList: string = "0,1,2,3,4,5,6,7,8,9,10,11";
    /**
     * 承运单编号
     */
    carrierOrderId: string;

    skip:number=0;


    constructor($scope: IFeeManagentViewModel,
        private feeService: IFeeService,
        private valueService: IValueService,
        private orderService: IorderService,
        private $ngBootbox: BootboxService,
        private $state: angular.ui.IStateService,
        private authorizationService: IAuthorizationService,
        private routeService:routeService,
        private employeeService: IEmployeeService,
        private $http: ng.IHttpService,
        private $location:ng.ILocationService,
        private clientService:clientService,
        private exportService:IexportService
        ) 
        {
        super($scope);
        this.init();
    }

    init():void{
        super.init();

        //应付管理导出权限
        this.$scope.canExcel = false;
        this.authorizationService.auth("24E0A026891BC3903C7917417F680C9D0816BC4ADCCBC945B81F6A3A2C42FF17").then((res) => {
            this.$scope.canExcel = res.data.extData;
        }, rej => { })


        this.$scope.checkDetail = this.checkDetail;
        // this.$scope.excel = this.excel2;
        this.loadValueDropDown();
        this.$scope.searchClient = this.searchClient;
        this.$scope.clientSelect = this.clientSelect;
        this.$scope.inputChanged = this.inputChanged;
        this.$scope.queryTerms = {
            feeCode: "",
            client:"",
            carrierName:"",
            paymentMethod:"",
            contractNumber:"",
            startTime:"",
            endTime:"",
            carCode: "",
            feeType: "",
            carrierPhone:"",
            feeStatus:"",
            applicant: "",
            feeSettleBillCode:"",
            settleStartTime:"",
            settleEndTime:"",
            payStartTime:"",
            payEndTime:"",
            payUser: "",
        };



        this.$scope.gridOptions = {
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            exporterMenuCsv: false,
            useExternalSorting: true,
            enableSelectAll: false,//全选禁用
            enableColumnResizing: true,
            exporterMenuPdf: false,
            enableGridMenu: true,
            enableFullRowSelection: true,
            multiSelect: false,
            columnDefs: [
                { displayName: "应付总编号", field: 'feeSettleBillCode', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '8%' },
                { displayName: "费用编号", field: 'feeCode', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '8%' },
                { displayName: "合同号", field: 'contractNumber', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '结算承运商', field: 'carrier', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '承运商电话号', field: 'carrierPhone', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false, width: '8%' },
                { displayName: '车牌号', field: 'carCode', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '付款方式', field: 'paymentMethod', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false },
                { displayName: '费用类型', field: 'feeType', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '费用金额', field: 'feeAmount', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '费用状态', field: 'feeStatus', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '申领人员', field: 'applicant', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '审核人员', field: 'auditedUser', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '结算人员', field: 'reckonerName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '付款人员', field: 'payUser', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '申请日期', field: 'creationTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '7%' },
                { displayName: '结算日期', field: 'settleTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false, width: '7%' },
                { displayName: '付款日期', field: 'payTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false, width: '7%' }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope,(skipSize,pageSize)=>{
                    this.paginationChanged(skipSize,pageSize,this.$state);
                });
                // gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.core.notifyDataChange("options");
                gridApi.core.on.sortChanged(this.$scope, this.sortChange);
                gridApi.selection.on.rowSelectionChanged(this.$scope, this.selectChange);
            }
        }
        
        this.query();

        /*datetimepicker*/
        $('#openstartTime').click(function () {
            $('#startTime').datetimepicker('show');
        });
        $('#openendTime').click(function () {
            $('#endTime').datetimepicker('show');
        });
        $('#opensettleStartTime').click(function () {
            $('#settleStartTime').datetimepicker('show');
        });
        $('#opensettleEndTime').click(function () {
            $('#settleEndTime').datetimepicker('show');
        });
        $('#openpayStartTime').click(function () {
            $('#payStartTime').datetimepicker('show');
        });
        $('#openpayEndTime').click(function () {
            $('#payEndTime').datetimepicker('show');
        });

    }
    
    localHistory():void{
        super.localHistory(this.$state)
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }

    /**
     * 加载数据
     */
    loadData(queryTerms: IFeeManagentQueryTerms, skip: number, count: number): void {
        if(this.$scope.queryTerms.feeStatus == "" || !this.$scope.queryTerms.feeStatus){this.$scope.queryTerms.feeStatus = "0,1,2,3,4,5,6,7,8,9,10,11";}
        let feeStatuslist = this.$scope.queryTerms.feeStatus
        if(this.$scope.queryTerms.feeStatus == "10"){ feeStatuslist = "10,11";}

        this.feeService.getFinanceFeeList(this.orderBy.toString(), this.asc, queryTerms.feeSettleBillCode, queryTerms.feeCode, queryTerms.client, "", queryTerms.applicant, queryTerms.contractNumber, "", "", queryTerms.startTime, queryTerms.endTime, queryTerms.settleStartTime, queryTerms.settleEndTime, queryTerms.payStartTime, queryTerms.payEndTime, "", "", "", "", queryTerms.carrierName, queryTerms.carrierPhone, queryTerms.carCode, queryTerms.feeType, feeStatuslist, "", "", queryTerms.paymentMethod, queryTerms.payUser, skip, count).then(res => {
            // this.$scope.gridOptions.totalItems = res.data.total;
            this.$scope.gridOptions.data = res.data.data;
            /**
             * 设置列表高度
             */
            let gridHArr = 0;
            res.data.data.forEach((item,index) => { gridHArr =  gridHArr + item.orderList.length; });
            this.$scope.heightA = ((res.data.data.length)*39)+96;

            // if (res.data.extData != null) {
            //     this.$scope.CostTotal = res.data.extData;  //这里费用总和接口不对
            // }
            
            if (skip == 0) {
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }
            this.skip = skip;

            this.$scope.querying = false;
        }, error => {
            this.$scope.querying = false;
        })
    }

    inputChanged=(str)=>{
         this.$scope.queryTerms.client = str;
    }

    getListCount():void{
        if(this.$scope.queryTerms.feeStatus == "" || !this.$scope.queryTerms.feeStatus){this.$scope.queryTerms.feeStatus = "0,1,2,3,4,5,6,7,8,9,10,11";}
        let feeStatuslist = this.$scope.queryTerms.feeStatus
        if(this.$scope.queryTerms.feeStatus == "10"){ feeStatuslist = "10,11";}

        this.feeService.getFinanceFeeCount(this.$scope.queryTerms.feeSettleBillCode, this.$scope.queryTerms.feeCode, this.$scope.queryTerms.client, "", this.$scope.queryTerms.applicant, this.$scope.queryTerms.contractNumber, "", "", this.$scope.queryTerms.startTime, this.$scope.queryTerms.endTime, this.$scope.queryTerms.settleStartTime, this.$scope.queryTerms.settleEndTime, this.$scope.queryTerms.payStartTime, this.$scope.queryTerms.payEndTime, "", "", "", "", this.$scope.queryTerms.carrierName, this.$scope.queryTerms.carrierPhone, this.$scope.queryTerms.carCode, this.$scope.queryTerms.feeType, feeStatuslist, "", "", this.$scope.queryTerms.paymentMethod, this.$scope.queryTerms.payUser).then(res=>{
            this.$scope.gridOptions.totalItems = res.data.extData;
        });
        this.getListFeeTotal();
    }

    getListFeeTotal():void{
        if(this.$scope.queryTerms.feeStatus == "" || !this.$scope.queryTerms.feeStatus){this.$scope.queryTerms.feeStatus = "0,1,2,3,4,5,6,7,8,9,10,11";}
        let feeStatuslist = this.$scope.queryTerms.feeStatus
        if(this.$scope.queryTerms.feeStatus == "10"){ feeStatuslist = "10,11";}

        this.feeService.getFinanceFeeTotal(this.$scope.queryTerms.feeSettleBillCode, this.$scope.queryTerms.feeCode, this.$scope.queryTerms.client, "", this.$scope.queryTerms.applicant, this.$scope.queryTerms.contractNumber, "", "", this.$scope.queryTerms.startTime, this.$scope.queryTerms.endTime, this.$scope.queryTerms.settleStartTime, this.$scope.queryTerms.settleEndTime, this.$scope.queryTerms.payStartTime, this.$scope.queryTerms.payEndTime, "", "", "", "", this.$scope.queryTerms.carrierName, this.$scope.queryTerms.carrierPhone, this.$scope.queryTerms.carCode, this.$scope.queryTerms.feeType, feeStatuslist, "", "", this.$scope.queryTerms.paymentMethod, this.$scope.queryTerms.payUser).then(res=>{
            if (res.data.extData != null) {
                this.$scope.CostTotal = res.data.extData;
            }
        })
    }



    /**
     * 排序事件触发
     */
    sortChange = (grid: uiGrid.IGridInstanceOf<FinanceFeeListItemResponse>, sortColumns: Array<uiGrid.IGridColumnOf<FinanceFeeListItemResponse>>) => {
        if (sortColumns.length == 0) {
            this.reload();
        } else {
            let sortItem = sortColumns[0];
            switch (sortItem.name) {
                case "feeCode": {
                    this.orderBy = 1;
                } break;
                case "feeSettleBillCode": {
                    this.orderBy = 2;
                } break;
                case "contractNumber": {
                    this.orderBy = 3;
                } break;
                case "contractStatus": {
                    this.orderBy = 12;
                } break;
                case "carrier": {
                    this.orderBy = 14;
                } break;
                case "carCode": {
                    this.orderBy = 11;
                } break;
                case "feeType": {
                    this.orderBy = 19;
                } break;
                case "feeAmount": {
                    this.orderBy = 15;
                } break;
                case "feeStatus": {
                    this.orderBy = 13;
                } break;
                case "applicant": {
                    this.orderBy = 18;
                } break;
                case "creationTime": {
                    this.orderBy = 0;
                } break;
                case "ReceiptStatus": {
                    this.orderBy = 9;
                } break;
            }
            this.asc = sortItem.sort.direction == "asc";
            this.reload();
        }
    }

    /**
    * 单选事件
    */
    selectChange = (row: uiGrid.IGridRowOf<FinanceFeeListItemResponse>) => {
        row.isSelected ? this.id = row.entity.id : this.id = "";
    }


    /**
    * 跳转费用对账页面
    */
    checkDetail = () => {
        if (this.id == ""||this.id==undefined) {
            this.$ngBootbox.alert("请选择一条查看详情");
        } else {
            this.feeService.feeDetail(this.id).then(res => {
                this.carrierOrderId = res.data.carrierOrderId;
                this.orderService.carryOrder(this.carrierOrderId).then(res => {
                    if (res.data.orderList.length > 1) {
                        this.$state.go("app.fee.feeBreakbulkDetail", { feeId: this.id, carrierOrderId: this.carrierOrderId, pageName: 'feeManage', feeStatus: this.feeStatusList })
                    } else {          //跳转的状态码需要修改
                        this.$state.go("app.fee.feeOrderDetailController", { feeId: this.id, orderId: res.data.orderList[0], carrierOrderId: this.carrierOrderId, name: 'FeeManage' })
                    }                 //跳转的状态码需要修改
                })
            })
        }
    }

    /**
     * 客户单位自动补全
     */
    searchClient=(value:string)=>{
        return this.clientService.getList(value,"", "", "","","",0,5).then((res) => {
            return res.data.data;
        });
    }

    clientSelect =(selected:angucomplete<ClientListItemResponse>)=>{
        if (selected) {
            this.queryTerms.client = selected.description.name;
        }
    };
    

    /**
    * 导出报表事件
    */
    exportFunc=()=>{
        if(this.$scope.gridOptions.totalItems>60000){
            this.$ngBootbox.alert('导出数据不可超过6万条');
            return;
        }else{
            if(status == null){ status=""; };
            if(this.$scope.queryTerms.feeStatus == "" || !this.$scope.queryTerms.feeStatus){this.$scope.queryTerms.feeStatus = "0,1,2,3,4,5,6,7,8,9,10,11";}
            let feeStatuslist = this.$scope.queryTerms.feeStatus
            if(this.$scope.queryTerms.feeStatus == "10"){ feeStatuslist = "10,11";}
            this.exportService.exportFinanceFeeList(Number(this.orderBy),this.asc,this.$scope.queryTerms.feeSettleBillCode,this.$scope.queryTerms.feeCode,this.$scope.queryTerms.client,"", this.$scope.queryTerms.applicant, this.$scope.queryTerms.contractNumber,"","",this.$scope.queryTerms.startTime,this.$scope.queryTerms.endTime,this.$scope.queryTerms.settleStartTime,this.$scope.queryTerms.settleEndTime,this.$scope.queryTerms.payStartTime,this.$scope.queryTerms.payEndTime,"","","","",this.$scope.queryTerms.carrierName,this.$scope.queryTerms.carrierPhone,this.$scope.queryTerms.carCode,this.$scope.queryTerms.feeType,feeStatuslist,"","",this.$scope.queryTerms.paymentMethod,this.$scope.queryTerms.payUser);
        }
        
    }



    /**
     * 所有可枚举下拉
     */
    loadValueDropDown = () => {
        this.$scope.paymentMethodList = this.valueService.getPaymentMethodList().data;
        this.$scope.feeTypeList = this.valueService.getFeeTypeList().data;
        this.$scope.feeStatusEnumList = this.valueService.getFeeStatusList().data;
    }

}

angular.module("tms").controller("feeManagentController", ["$scope", "feeService", "valueService", "orderService",
    "$ngBootbox", "$state", "authorizationService","routeService",
    "employeeService", "$http", "$location","clientService","exportService",FeeManagentController])